﻿package com.ufenlei.LTS{
	import flash.display.MovieClip;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	
	public class TestingPlayer extends MovieClip{
		private var Pbar:MovieClip;
		private var Lbar:MovieClip;
		private var Tbar:MovieClip;
		private var PHandle:MovieClip;
		private var LHandle:MovieClip;
		private var vs:VolumeSlide;
		private var vb:Switch;
		private var Pbtn:TestingSwitch;
		private var _volume:Number = 1.0;
		public var sf:SoundFacade;
		public function TestingPlayer() {
			sf= new SoundFacade(true, 1000);
			Pbar = playedBar;
			trace(Pbar,"***************");
			Lbar = loadedBar;
			Tbar = totalBar;
			PHandle = handle;
			LHandle = loadedBarTop;
			Pbtn = playBtn;
			vs = volumeSlide;
			vb = volumeBtn;
			initEvent();
			Pbar.width = 0;
			Lbar.width = 0;
			vs.pecentage = 1.0;
		}
		private function initEvent():void{
			Pbtn.addEventListener(Input.UNSELECTED ,onPause);
			Pbtn.addEventListener(Input.SELECTED ,onPlay);
			//this.loadbtn.addEventListener(MouseEvent.CLICK,onLoadBtn);
			this.sf.addEventListener(SoundFacade.PLAY_PROGRESS,onPlayProgress);
			this.sf.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			this.sf.addEventListener(SoundFacade.PLAY_STOP,onPlayStop);
			this.sf.addEventListener(Event.COMPLETE,onLoadComplete);
			this.sf.addEventListener(Event.OPEN, onLoadOpen);
			this.vb.addEventListener(Input.SELECTED , onVolumeOff);
			this.vb.addEventListener(Input.UNSELECTED , onVolumeOn);
			body.addEventListener(MouseEvent.MOUSE_DOWN, onbodyMD);
			vs.addEventListener(Input.CHANGE , onVolumeSlideChange);
		}
		private function onLoadOpen(evt:Event):void{
			trace('onLoadOpen');
			Pbtn.showSelect();
		}
		private function onbodyMD(evt:MouseEvent):void{
			this.startDrag();
			(root as MovieClip).addEventListener(MouseEvent.MOUSE_UP, onbodyMU);
		}
		private function onbodyMU(evt:MouseEvent):void{
			this.stopDrag();
		}
		private function onVolumeSlideChange(evt:ProgressEvent):void{
			setVolume(evt.bytesLoaded/evt.bytesTotal);
		}
		private function onVolumeOff(evt:Event):void{
			_volume = this.sf.sc.soundTransform.volume;
			trace("Off",_volume);
			setVolume(0);
		}
		private function onVolumeOn(evt:Event):void{
			setVolume(_volume);
			trace("on",_volume);
		}
		private function setVolume(v:Number = 1):void{
			sf.volume = v;
		}
		private function onPlayProgress(evt:ProgressEvent):void{
			var pgs:Number = evt.bytesLoaded/evt.bytesTotal;
			this.Pbar.width = this.Tbar.width*pgs;
			this.PHandle.x = this.Pbar.x + this.Pbar.width;
		}

		private function onLoadComplete(evt:Event):void{
			//Pbtn.select();
			this.Lbar.width = this.Tbar.width;
			this.LHandle.x = Lbar.x + Lbar.width;
		}
		private function onLoadProgress(evt:ProgressEvent):void{
			var pgs:Number = evt.bytesLoaded/evt.bytesTotal;
			this.Lbar.width = this.Tbar.width*pgs ;
			this.LHandle.x = Lbar.width + Lbar.x;
		}

		private function onPlayStop(evt:Event):void{
			this.Pbar.width =0;
			this.PHandle.x = this.Pbar.x;
		}
		private function onPlay(evt:Event):void{
			this.sf.resume();
		}
		private function onPause(evt:Event):void{
			this.sf.pause();
		}
		private function onLoadBtn(evt:Event):void{
		}
		public function load(soundURL:String):void{
			this.sf.load(soundURL);
		}
	}
}